package top.deepdesigner.controller.promotion;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsTopic;
import top.deepdesigner.service.DtsTopicService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/23 星期五 11:45:00 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/topic")
public class TopicController {

  @Autowired
  private DtsTopicService topicService;

  /**
   * 专题列表
   *
   * @param params 参数个数
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:topic:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");
    String title = (String) params.get("title");
    String subtitle = (String) params.get("subtitle");

    CommonListVO<DtsTopic> topicListVO = new CommonListVO<>();
    //获取分页数量
    Long count = topicService.getTopicCount();
    List<DtsTopic> topicList = topicService.getTopicList(page, limit, sort, order, title, subtitle);
    topicListVO.setTotal(count);
    topicListVO.setItems(topicList);
    return ResponseUtil.ok(topicListVO);
  }

  /**
   * 创建专题
   *
   * @param topic 主题
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "添加")
  @RequiresPermissions(value = {"admin:topic:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsTopic topic) {
    DtsTopic couponRes = topicService.createTopic(topic);
    if (couponRes != null) return ResponseUtil.ok(couponRes);
    return ResponseUtil.fail();
  }

  /**
   * 更新专题
   *
   * @param topic 主题
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "修改")
  @RequiresPermissions(value = {"admin:topic:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsTopic topic) {
    DtsTopic couponRes = topicService.updateTopic(topic);
    if (couponRes != null) return ResponseUtil.ok(couponRes);
    return ResponseUtil.fail();
  }

  /**
   * 删除专题
   *
   * @param topic 主题
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "删除")
  @RequiresPermissions(value = {"admin:topic:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsTopic topic) {
    topicService.deleteTopic(topic);
    return ResponseUtil.ok();
  }
}
